package cn.carl.frame.core.mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.carl.frame.constants.FrameConstants;
import cn.carl.frame.core.config.Route;

@WebServlet(initParams = {}, urlPatterns = "/", loadOnStartup = 1, asyncSupported = true)
public class DispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = -6L;

	@Override
	public void init(ServletConfig config) throws ServletException {
		// route不为空则不执行
		if (Objects.nonNull(FrameConstants.route)) {
			return;
		} else {
			// 路由模型
			String route_config = config.getInitParameter("route");
			try {
				Class<?> route_class = Class.forName(route_config);
				// 读取配置文件
				Route route = (Route) route_class.newInstance();
				FrameConstants.route = route;
				List<Pattern> patterns = FrameConstants.route.getPatterns();
				patterns.forEach(System.out::println);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");// 设置req的编码为UTF-8
		res.setCharacterEncoding("UTF-8");// 设置将字符以"UTF-8"编码输出到客户端浏览器
		StringBuffer reqUrl = req.getRequestURL();// 得到请求的URL地址
		String reqUri = req.getRequestURI();// 得到请求的资源
		String queryString = req.getQueryString();// 得到请求的URL地址中附带的参数
		String remoteAddr = req.getRemoteAddr();// 得到来访者的IP地址
		String remoteHost = req.getRemoteHost();
		int remotePort = req.getRemotePort();
		String remoteUser = req.getRemoteUser();
		String method = req.getMethod();// 得到请求URL地址时使用的方法
		String pathInfo = req.getPathInfo();
		String localAddr = req.getLocalAddr();// 获取WEB服务器的IP地址
		String localName = req.getLocalName();// 获取WEB服务器的主机名
		Map<?, ?> parameterMap = req.getParameterMap();
		Enumeration<String> parameterNames = req.getParameterNames();
		String contextPath = req.getContextPath();
		Enumeration<Locale> locales = req.getLocales();
		// 通过设置响应头控制浏览器以UTF-8的编码显示数据，如果不加这句话，那么浏览器显示的将是乱码
		res.setHeader("content-type", "text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();
		out.write("获取到的客户机信息如下：");
		out.write("<hr/>");
		out.write("parameterMap" + parameterMap);
		out.write("<br/>");
		out.write("parameterNames" + parameterNames);
		out.write("<br/>");
		out.write("contextPath" + contextPath);
		out.write("<br/>");
		out.write("locales" + locales);
		out.write("请求的URL地址：" + reqUrl);
		out.write("<br/>");
		out.write("请求的资源：" + reqUri);
		out.write("<br/>");
		out.write("请求的真实资源：" + reqUri.replaceFirst(contextPath, ""));
		out.write("<br/>");
		out.write("请求的URL地址中附带的参数：" + queryString);
		out.write("<br/>");
		out.write("来访者的IP地址：" + remoteAddr);
		out.write("<br/>");
		out.write("来访者的主机名：" + remoteHost);
		out.write("<br/>");
		out.write("使用的端口号：" + remotePort);
		out.write("<br/>");
		out.write("remoteUser：" + remoteUser);
		out.write("<br/>");
		out.write("请求使用的方法：" + method);
		out.write("<br/>");
		out.write("pathInfo：" + pathInfo);
		out.write("<br/>");
		out.write("localAddr：" + localAddr);
		out.write("<br/>");
		out.write("localName：" + localName);
	}

}
